﻿using AutoMapper;
using MediatR;
using Reporting.API.Application.CommandHandler.Productionorders;
using Reporting.API.Application.Commands.ForeignKey;
using Reporting.Domain.ForeignKey;
using Reporting.Errorcode.ErrorCode;
using Reporting.Infrastructure.Interfaces.Productionorders;
using Reporting.Infrastructure.Interfaces.Sourcetypes;

namespace Reporting.API.Application.CommandHandler.ForeignKey
{
    /// <summary>
    /// 来源类型
    /// </summary>
    public class SourcetypeCommondHandler : IRequestHandler<ForeignKeyCommond, ApiResult<IList<Sourcetype>>>
    {
        private readonly ISourcetypeRepository  _sourcetypeRepository;
        private readonly IMapper _mapper;
        private readonly ILogger<SourcetypeCommondHandler> _logger;

        public SourcetypeCommondHandler(ILogger<SourcetypeCommondHandler> logger, IMapper mapper, ISourcetypeRepository sourcetypeRepository)
        {
            _logger = logger;
            _mapper = mapper;
            _sourcetypeRepository = sourcetypeRepository;
        }

        public async Task<ApiResult<IList<Sourcetype>>> Handle(ForeignKeyCommond request, CancellationToken cancellationToken)
        {
            try
            {
                // 获取所有生产计划
                var ProductionPlans = await _sourcetypeRepository.GetAllAsync();

                var result = _mapper.Map<IList<Sourcetype>>(ProductionPlans);

                // 返回成功结果
                return ApiResult<IList<Sourcetype>>.Success(result, StatusCodeEnum.Success);
            }
            catch (Exception ex)
            {

                // 返回失败结果
                return ApiResult<IList<Sourcetype>>.Fail("查询失败：" + ex.Message, StatusCodeEnum.BadRequest);
            }
        }
    }
}
